package com.service.impl;

import com.common.exception.db.UpdateException;
import com.common.paging.PagingData;
import com.dao.BillDao;
import com.dao.ZzmDao;
import com.entity.MoneytobankInfo;
import com.service.ZzmService;
import com.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ZzmServiceimpl implements ZzmService{
    @Autowired
    private ZzmDao zzmDao;
    @Autowired
    private BillDao billDao;

    @Override
    public PagingData findWithdraw(Integer page,Integer length) {
        List<MoneytobankInfo> list = zzmDao.findWithdraw(page,length);
        int count = zzmDao.findRefundRecordCount();
        PagingData pagingData = new PagingData();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return pagingData;
    }

    @Override
    @Transactional
    public int updateWithdrawState(Integer id,Integer userId,BigDecimal money,Integer type) {
        int res=zzmDao.updateWithdrawState(id,type);
        if(res > 0){
            if(type == 1){
                String uuid = UUIDUtil.get32UUID();
                res = billDao.insertBill(userId,uuid,money,2,"银行卡提现");
                if(res > 0){
                    return res;
                }else{
                    throw new UpdateException("新增流水失败");
                }
            }else{
               res = billDao.updateUserMoney1(userId,money);
               if(res > 0){
                   return res;
               }else{
                   throw new UpdateException("修改失败");
               }
            }
        }else {
            throw new UpdateException("修改状态失败");
        }
    }

    @Override
    public int findorder() {
        return zzmDao.findorder();
    }

    @Override
    public int updateGoldPay(Integer userId, BigDecimal discountMoney) {
        return zzmDao.updateGoldPay(userId,discountMoney);
    }

}
